//旺店通编辑名称弹窗
import React, { useState } from 'react';
import CommonModal from '@/components/CommonModal';
import CommonLoading from '@/components/CommonLoading';
import { Input, Form, message, Select } from 'antd';
import { wdtAuthToken } from '@/services/common';
import { useNavigate } from 'react-router-dom';

const layout1 = {
  //发布表单排布
  labelCol: {
    span: 8,
  },
  wrapperCol: {
    span: 16,
  },
};

const { Option } = Select;

const AddWangDianTong = ({ visible, handleVisible, handleOk }) => {
  const [form] = Form.useForm();
  const [loading, setLoading] = useState(false);
	const navigate = useNavigate();
  const handleSubmit = async () => {
    setLoading(true);
    const { storeName, syncGoodsType, appKey, appSecret, token } =
      form.getFieldsValue();
    const res = await wdtAuthToken({
      storeName,
      syncGoodsType,
      appKey,
      appSecret,
      token,
      type: 27,
    });
    setLoading(false);
    if (res.success) {
      message.success('添加成功！');
      navigate(0);
    }
  };

  const onOk = () => {
    form.validateFields().then((val) => {
      handleSubmit();
    });
  };

  return (
    <CommonModal
      title="店铺授权"
      visible={visible}
      onCancel={() => handleVisible(false)}
      onOk={onOk}
      minHeight="150px"
      width="600px"
    >
      <Form {...layout1} form={form} name="add-wangdiantong-store">
        <Form.Item
          name="storeName"
          label="旺店通店铺"
          rules={[
            { required: true, message: '店铺名不能为空' },
            { max: 7, message: '不能超过7个字符' },
          ]}
        >
          <Input placeholder="请输入名称" />
        </Form.Item>
        <Form.Item
          name="syncGoodsType"
          label="首次同步商品类型"
          rules={[{ required: true, message: '请选择商品类型' }]}
        >
          <Select placeholder="请输入名称">
            <Option value="1">平台货品</Option>
            <Option value="2">系统货品</Option>
          </Select>
        </Form.Item>
        <Form.Item
          name="appKey"
          label="appkey"
          rules={[{ required: true, message: 'appkey不能为空' }]}
        >
          <Input placeholder="请输入名称" />
        </Form.Item>
        <Form.Item
          name="appSecret"
          label="appsecret"
          rules={[{ required: true, message: 'appsecret不能为空' }]}
        >
          <Input placeholder="请输入名称" />
        </Form.Item>
        <Form.Item
          name="token"
          label=" sid（卖家账号）"
          rules={[{ required: true, message: 'sid不能为空' }]}
        >
          <Input placeholder="请输入名称" />
        </Form.Item>
      </Form>
      <CommonLoading loading={loading} />
    </CommonModal>
  );
};

export default AddWangDianTong;
